﻿using System;
using System.Collections.Generic;
using System.Web;
using ProcessControl;
using System.Data;
using MyLib;
using System.Text;

namespace UseCar
{
    /// <summary>
    /// FindCar 的摘要说明
    /// </summary>
    public class FindCar : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strat = context.Request.QueryString["start"].ToString();    
            string end = context.Request.QueryString["end"].ToString();
            string carId = context.Request.QueryString["carId"].ToString();
            string incident = context.Request.QueryString["incident"].ToString();

            //获取两个日期间的天数（无时间）
            DateTime dt1 = Convert.ToDateTime(strat);
            DateTime dt2 = Convert.ToDateTime(end);
            TimeSpan ts = (TimeSpan)(dt2 - dt1);
            int TempDays = Convert.ToInt32(ts.TotalDays);
            bool b = true;
            string sWrite = "";
            for (int i = 0; i <= TempDays; i++)
            {

                string sDay = Convert.ToDateTime(strat).AddDays(i).ToString("yyyy-MM-dd");
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1 and b.incident not in {2}", sDay, carId, incident);
                DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(sb.ToString());
                if (dt.Rows.Count > 0)
                {

                    b = false;
                    sWrite = sDay.ToString();
                }
            }
            context.Response.Write(sWrite);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}